<?php 

//this script checks to see if the books are checked out and if not checks them out
require_once('auth.php');
//this is where we invoke the include.php file 
require 'connect.php';

//This gets info on the book
$barcode = $_POST['barcode'];
$book = mysql_query("SELECT * FROM books WHERE barcode = '$barcode'");
$book_get= mysql_fetch_array($book);
$bookdue= $book_get['duedate'];
$name= $book_get['name'];
$checkout = $book_get['checkout'];
$bookpatron = $book_get['outto'];
//Patron Ifomation

$patronsql = mysql_query("SELECT * FROM patrons WHERE code = '$bookpatron'");
$patroninfo = mysql_fetch_array($patronsql);
$patronname = $patroninfo['name'];
$patronfine = $patroninfo['fines'];

//this checks to see if the book exists


$result = mysql_query("SELECT name FROM books WHERE barcode='$barcode'");
$num_rows = mysql_num_rows($result);



if ($num_rows == 0 ) { 
    header("Location: p=?found=false&bar=" . $barcode);
} 

if ($checkout == 'false')
{

  header("Location: index.php?status=notout&bar=$barcode");
  

}





if ($num_rows == 1 ) {
$checkin1sql = "UPDATE books SET outto = '' WHERE barcode= $barcode";
$checkin2sql = "UPDATE books SET checkout = 'false' WHERE barcode= $barcode";
$checkin3sql = "UPDATE books SET duedate = '' WHERE barcode = $barcode";
//this inserts the data into the database
mysql_query($checkin1sql);
mysql_query($checkin2sql);
mysql_query($checkin3sql);
//This returns the user to the checkout page
header("Location: /olms/?p=checkin/index.php&status=checked&bar=$barcode&bookname=$name");

}
else {
header("Location: /olms/?p=checkin/index.php&status=notfound&bar=" . $barcode);
} 
?>

